var express = require('express');
const axios = require('axios');

var router = express.Router();

router.get('/', function (req, res, next) {
    // res.send("个人信息页面");
    res.render('app/add');
})

/**
 * @api {post} /add/upPost 发布帖子
 * @apiName 发布页面
 * @apiGroup Add
 *
 * @apiParam {String} pageNumber 第几页.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/upPost', function (req, res, next) {
    let { postData, postTitle, kind } = req.body
    let files = req.body.files
    let param = new URLSearchParams()
    param.append('picDataArr', files)
    param.append('postData', postData)
    param.append('postTitle', postTitle)
    param.append('userId', req.session.user.id)
    param.append('kind', kind)
    param.append('userName', req.session.user.nickname)
    console.log(req.session.user.id)
    var config = {
        method: 'post',
        baseURL: 'http://localhost:9022',
        url: '/tb-post/upPost',
        // data: {
        //     picDataArr: files,
        //     postData: postData,
        //     postTitle: postTitle,
        //     userId: req.session.user.id,
        //     // postion: postion,
        //     kind: kind,
        //     userName: req.session.user.nickname
        // },
        data: param,
        // headers: {//指定响应头
        //     "Content-Type": "application/json;charset=utf-8",
        //     "Accept": "application/json"
        // }
    }
    axios(config)
        .then(function (response) {
            // console.log(response.data);
            if (response.data.code == 'success') {
                console.log("成功")
                res.send({ err: 0, msg: "发布成功", data: response.data })
            } else {
                // console.log(response);
                res.send({ err: -1, msg: "发布失败" })
                console.log("失败")
            }
        })
        .catch(function (error) {
            // console.log(error);
            res.send({ err: -1, msg: "服务器错误" })
            console.log("失败")
        });

})



module.exports = router; /*模块化暴露*/